package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberMapper memberMapper;

    @Override
    public Member findByTelephone(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    @Override
    public Member add(Member member) {
        memberMapper.add(member);
        return member;//返回web才能拿到用户id
    }

    @Override
    public List<Member> getFamily(Integer id) {
        //根据id获取当前member的家庭联系人，并且在这个集合加上自己的
        List <Member> memberList = memberMapper.getFamily(id);
        Member member = memberMapper.selectByPrimaryKey(id);
        memberList.add(member);
        return memberList;
    }

    @Override
    public List<Map<String, Object>> findMemberAgeCount() {
        return memberMapper.findMemberAgeCount();
    }

    @Override
    public List<Map> getGenderReport() {
        List<Map> list = memberMapper.getGenderReport();
        List<Map> lists = new ArrayList<>();
        for (Map map : list) {

            Long value = (Long) map.get("count");
            map.put("value", value);
            String name = (String) map.get("sex");
            if (name == null) {
                map.put("name", "男女不限");
                lists.add(map);
                continue;
            }
            map.put("name", name.equals("1") ? "男" : "女");
            lists.add(map);

        }
       return lists;
    }
}
